<?php 
class Project_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    function get_real_projects(){
        $sql = '';
        if(is_admin()){
            $sql ="SELECT `id`,`name` FROM tblprojects WHERE `iscomplete`='0' ORDER BY `enddate` DESC";
        }else {
            $pn_id = $this->session->userdata['id'];
            $sql = "SELECT `id`,`name` FROM tblprojects WHERE `iscomplete`='0'
                    AND `id` IN 
                        (
                           SELECT DISTINCT `projectid` FROM `tblmanager` WHERE `personnelid`='$pn_id'
                        )
                    ORDER BY `enddate` DESC";
        }
        return 
            $this->db->query($sql)->result_array();
    }
    function get_name($id){
        $this->db->select('name');
        $this->db->from('tblprojects');
        $this->db->where('id',$id);
        $query = $this->db->get();
        $arr = $query->result_array();
        return $arr[0]['name'];
    }
    function get_project_from_manager($id){
        $this->db->select('projectid');
        $this->db->from('tblmanager');
        $this->db->where('tblmanager.projectid',$id);
        $query = $this->db->get();
        $arr = $query->result_array();
        return $arr[0]['id'];
    }
   
    function get_resources(){
//        $this->db->select('id,name');
//        $this->db->from('tblresources');
//        $query = $this->db->get();
//        $arr = $query->result_array();
//        return $arr;
        return $this->db->get('tblresources')->result_array();
    }
    function get_materials(){
       return $this->db->get('tblmaterials')->result_array();
    }
    function get_resources_materials_of_pn_project($managerid){// tra ve hai mang chua re va me
        $sql = "SELECT resourceid,tblresources.name as 'resourcename',amount
                 FROM tblassigns 
                 JOIN tblresources ON tblresources.id = resourceid
                 JOIN tblmanager ON managerid = tblmanager.id
                 WHERE tblmanager.id='$managerid'
                ";
        $arr = $this->db->query($sql)->result_array();
        $sql = "SELECT materialid,tblmaterials.name as 'materialname', amount,tblmaterials.unit
                 FROM tblassigns 
                 JOIN tblmaterials ON tblmaterials.id = materialid  
                 JOIN tblmanager ON managerid = tblmanager.id
                 WHERE tblmanager.id='$managerid'
                ";
        $arr2 = $this->db->query($sql)->result_array();
        return array($arr,$arr2);
    }
    function get_managerid_of_boss($pn_id,$pj_id,$workid){
        $this->db->select('id');
        $this->db->from('tblmanager');
        $this->db->where('personnelid',$pn_id);
        $this->db->where('projectid',$pj_id);
        $arr = $this->db->get()->result_array();
        return $arr[0]['id'];
    }
    
}
?>
